package com.yqkj.zysoft.upms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.easy.common.sequencer.SnowflakeIdWorker;
import  com.project.easy.common.util.collection.CollectionUtil;
import  com.project.easy.common.util.constants.CommonConstant;
import  com.project.easy.common.util.dto.CommonContextParam;
import  com.project.easy.common.util.enums.DelEnums;
import  com.project.easy.common.util.enums.StatusEnums;
import  com.project.easy.common.util.exception.business.BusinessException;
import  com.project.easy.common.util.string.StringUtil;
import  com.project.easy.common.util.tree.TreeUtil;
import com.yqkj.zysoft.upms.entity.FrameworkFunction;
import com.yqkj.zysoft.upms.entity.dto.FrameWorkFunctionAddRequest;
import com.yqkj.zysoft.upms.entity.dto.FrameWorkFunctionUpdateRequest;
import com.yqkj.zysoft.upms.entity.dto.FunctionTreeDto;
import com.yqkj.zysoft.upms.entity.dto.QueryFunctionRequest;
import com.yqkj.zysoft.upms.mapper.FrameworkFunctionMapper;
import com.yqkj.zysoft.upms.service.IFrameworkFunctionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author GreatorMan.cool
 * @since 2022-08-10
 */
@Service
public class FrameworkFunctionServiceImpl extends ServiceImpl<FrameworkFunctionMapper, FrameworkFunction> implements IFrameworkFunctionService {

    @Autowired
    private  FrameworkFunctionMapper frameworkFunctionMapper;
    /**
     * @param commonContextParam
     * @return
     */
    @Override
    public List<FunctionTreeDto> menu(CommonContextParam commonContextParam) {
        List<FrameworkFunction> frameworkFunctions = frameworkFunctionMapper.queryFunctionByUserId(commonContextParam.getApiContextParamUserInfo().getUserId());
        List<FunctionTreeDto> result = new ArrayList<>(CollectionUtil.listSize(frameworkFunctions));
        if (!CollectionUtil.isNull(frameworkFunctions)) {
            for (FrameworkFunction frameworkFunction: frameworkFunctions) {
                FunctionTreeDto functionTreeDto = new FunctionTreeDto(frameworkFunction.getFuncCode(), frameworkFunction.getIoc());
                functionTreeDto.setId(frameworkFunction.getId());
                functionTreeDto.setpId(frameworkFunction.getParentId());
                functionTreeDto.setName(frameworkFunction.getFuncName());
                functionTreeDto.setSort(frameworkFunction.getSort());
                result.add(functionTreeDto);
            }
        }
        List<FunctionTreeDto> bulid = TreeUtil.bulid(result, CommonConstant.ROOT_ID);
        return bulid;
    }

    /**
     * 新增
     *
     * @param workFunctionAddRequest 参数
     * @return 返回 true 成功 false 失败
     */
    @Override
    public Long add(FrameWorkFunctionAddRequest workFunctionAddRequest) {
        String innerCode = SnowflakeIdWorker.nextIdStr();
        Long parentId = CommonConstant.ROOT_ID;

        if (StringUtil.isNotBlank(workFunctionAddRequest.getParentCode())) {
            QueryWrapper<FrameworkFunction> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("innser_code", workFunctionAddRequest.getParentCode());
            FrameworkFunction frameworkFunction = this.frameworkFunctionMapper.selectOne(queryWrapper);
            if (Objects.isNull(frameworkFunction)) {
                throw  new BusinessException("功能非法");
            }
            parentId = frameworkFunction.getId();
            innerCode = String.format("%s|%s", frameworkFunction.getInnserCode(), innerCode);
        }

        FrameworkFunction frameworkFunction = new FrameworkFunction();
        BeanUtils.copyProperties(workFunctionAddRequest, frameworkFunction);
        frameworkFunction.setInnserCode(innerCode);
        frameworkFunction.setParentId(parentId);

        boolean save = this.save(frameworkFunction);
        if (save) {
            return frameworkFunction.getId();
        }
        throw  new BusinessException("存储失败");
    }

    /**
     * 更新数据
     *
     * @param frameWorkFunctionUpdateRequest 参数
     * @return true 成功 false 失败
     */
    @Override
    public Boolean updateObject(FrameWorkFunctionUpdateRequest frameWorkFunctionUpdateRequest) {
        FrameworkFunction frameworkFunction = this.getFrameworkFunction(frameWorkFunctionUpdateRequest.getId());
        BeanUtils.copyProperties(frameWorkFunctionUpdateRequest, frameworkFunction, "parentId");
        return  this.updateById(frameworkFunction);
    }
    /**
     * 根据部门ID 获取数据
     * @param id ID
     * @return 返回部门数据
     */
    private FrameworkFunction getFrameworkFunction(Long id) {
        if (Objects.isNull(id)) {
            throw  new BusinessException("ID不能为空");
        }
        FrameworkFunction frameworkFunction = this.frameworkFunctionMapper.selectById(id);
        if (Objects.isNull(frameworkFunction)) {
            throw  new BusinessException("ID非法");
        }
        if (frameworkFunction.getHasDel()) {
            throw  new BusinessException("非法数据,已经删除");
        }
        return frameworkFunction;
    }
    /**
     * 分页
     *
     * @param queryFunctionRequest 分页查询条件
     * @return 返回数据
     */
    @Override
    public List<FunctionTreeDto> tree(QueryFunctionRequest queryFunctionRequest) {
        QueryWrapper<FrameworkFunction> queryWrapper = new QueryWrapper<>();
        Boolean isTree = Boolean.TRUE;
        if (StringUtil.isNotBlank(queryFunctionRequest.getName())) {
            isTree = Boolean.FALSE;
            queryWrapper.eq("func_name", queryFunctionRequest.getName());
        }
        queryWrapper.eq("has_del", DelEnums.NOT_DEL.getCode());
        queryWrapper.orderByAsc("parent_id", "sort");
        List<FrameworkFunction> frameworkFunctions = this.frameworkFunctionMapper.selectList(queryWrapper);
        if (!CollectionUtil.isNull(frameworkFunctions)) {
            List<FunctionTreeDto>  result = new ArrayList<>(CollectionUtil.listSize(frameworkFunctions));
            for (FrameworkFunction frameworkFunction : frameworkFunctions) {
                FunctionTreeDto functionTreeDto = new FunctionTreeDto(frameworkFunction.getFuncCode(), frameworkFunction.getIoc());
                BeanUtils.copyProperties(frameworkFunction, functionTreeDto);
                functionTreeDto.setpId(frameworkFunction.getParentId());
                functionTreeDto.setName(frameworkFunction.getFuncName());
                functionTreeDto.setId(frameworkFunction.getId());
                functionTreeDto.setIcon(frameworkFunction.getIoc());
                functionTreeDto.setFuncMethod(frameworkFunction.getFuncMethod());
                functionTreeDto.setValue(frameworkFunction.getInnserCode());
                functionTreeDto.setLabel(frameworkFunction.getFuncName());
                functionTreeDto.setStatus(frameworkFunction.getStatus());
                result.add(functionTreeDto);
            }
            if (isTree) {
                return  TreeUtil.bulid(result, CommonConstant.ROOT_ID);
            } else {
                return result;
            }
        }
        return CommonConstant.EMPTY_LIST;
    }

    /**
     * 删除菜单功能
     *
     * @param id ID
     * @return 返回 true 成功 false 失败
     */
    @Override
    public Boolean del(Long id) {
        FrameworkFunction frameworkDepart = getFrameworkFunction(id);

        String innerCode = frameworkDepart.getInnserCode();
        QueryWrapper<FrameworkFunction> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("inner_code", String.format("%s|", innerCode));
        queryWrapper.eq("has_del", DelEnums.NOT_DEL.getCode());
        List<FrameworkFunction> frameworkFunctions = this.frameworkFunctionMapper.selectList(queryWrapper);
        if (!CollectionUtil.isNull(frameworkFunctions)) {
            throw  new BusinessException("下级菜单有数据不能删除!");
        }
        frameworkDepart.setHasDel(Boolean.TRUE);
        this.frameworkFunctionMapper.updateById(frameworkDepart);
        return Boolean.TRUE;
    }

    /**
     * 启用
     *
     * @param id ID
     * @return true 启用成功 false 启用失败
     */
    @Override
    public Boolean able(Long id) {
        FrameworkFunction frameworkFunction = this.getFrameworkFunction(id);

        if (frameworkFunction.getStatus()) {
            throw  new BusinessException("启用不能再次启用!");
        }
        frameworkFunction.setStatus(Boolean.TRUE);
        this.baseMapper.updateById(frameworkFunction);
        return Boolean.TRUE;
    }

    /**
     * 启用
     *
     * @param id ID
     * @return true 启用成功 false 启用失败
     */
    @Override
    public Boolean disAble(Long id) {
        FrameworkFunction frameworkFunction = this.getFrameworkFunction(id);
        QueryWrapper<FrameworkFunction> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("inner_code", String.format("%s|", frameworkFunction.getInnserCode()));
        queryWrapper.eq("has_del", DelEnums.NOT_DEL.getCode());
        queryWrapper.eq("has_status", StatusEnums.ENBATL.getCode());
        List<FrameworkFunction> frameworkFunctions = this.frameworkFunctionMapper.selectList(queryWrapper);
        if (!CollectionUtil.isNull(frameworkFunctions)) {
            throw  new BusinessException("下级功能有启用的数据不禁用当前数据!");
        }
        frameworkFunction.setStatus(Boolean.FALSE);
        this.baseMapper.updateById(frameworkFunction);
        return Boolean.TRUE;
    }
}
